<template>
	<view>
		<!-- 状态栏 -->
		<view class="statusBar" :style="'height:'+statusBarHeight+'px'"></view>
		<view class="myheader padding-sm font-sub-title">
			<text class="cuIcon-back margin-right-ssm" @click="back()"></text>
			确认绑定
		</view>
		<view>
			<view class="margin-top margin-bottom margin-left font-normal">
				设备号：{{ code }}
			</view>
			<view class="margin-top text-red margin-bottom margin-left font-small">
				(Tips:最多关注5个设备)
			</view>
			<view class="text-right">
				<button class="cu-btn bg-orange margin-right font-normal" style="border-radius: 0;" @tap="follow()" >添加关注</button>
				<button class="cu-btn bg-blue margin-right font-normal" style="border-radius: 0;" @tap="confirm()" v-if="!user.IMEI">确认绑定</button>
				<button class="cu-btn bg-blue margin-right font-normal" style="border-radius: 0;" @tap="change()" v-if="user.IMEI">更换绑定</button>
			</view>
		</view>
	</view>
</template>

<script>
	import Vue from 'vue'
	import _config from '@/config/index.js'
	var that;
	export default {
		data() {
			return {
				code: '',
				user:'',
				statusBarHeight:Vue.prototype.StatusBar
			}
		},
		onLoad(data) {
			that = this;
			this.code = data.code
			this.user = uni.getStorageSync("userInfo")
		},
		methods: {
			back(){
				uni.navigateBack();
			},
			follow() {
				uni.showModal({
				    title: '温馨提示',
				    content: '您确认申请关注该设备吗？',
				    success: function (res) {
				        if (res.confirm) {
							that.$http.applyFollow(
							{
								imei: that.code
							}).then((res) => {
								if (res.code == 1) {
									uni.showToast({
										icon: 'none',
										position:'center',
										title: '申请关注成功，请等待回复！'
									});
								}else{
									uni.showToast({
										icon: 'success',
										title: res.message,
										position:"bottom"
									});
								}
							})
				        }
				    }
				});
			},
			change() {
				uni.showModal({
				    title: '温馨提示',
				    content: '您确认更换绑定为该设备吗？',
				    success: function (res) {
				        if (res.confirm) {
				            that.$http.changeBind(
				            {
				            	IMEI: that.code
				            }).then((res) => {
				            	if (res.code == 1) {
				            		uni.showToast({
				            			icon: 'success',
				            			title: '更换绑定成功！'
				            		});
									var autoInfo = uni.getStorageSync("autoInfo")
									var params = {
										phone : '',
										password : '',
										loginType:"1"
									}
									if(autoInfo!=null){
										params.phone = autoInfo.phone;
										params.password = autoInfo.password;
									}
									that.$store.dispatch('login', params).then(res => {
										if (res.code === 1) {
											uni.setStorageSync("token",res.result.token)
										} else {
											uni.showToast({
												icon: 'none',
												position: 'center',
												title: "登录信息失效，请重新登录"
											});
											uni.navigateTo({
												url: '/pages/login/login',
											})
										}
									}).catch(() => {
									})
				            	}else{
									uni.showToast({
										icon: 'success',
										title: res.message,
										position:"bottom"
									});
								}
				            })
				        }
				    }
				});
			},
			confirm() {
				uni.showModal({
				    title: '温馨提示',
				    content: '您确认绑定该设备吗？',
				    success: function (res) {
				        if (res.confirm) {
				            that.$http.bindEquipment(
				            {
				            	IMEI: that.code,
				            	PATIENT_ID: that.user.PATIENT_ID
				            }).then((res) => {
				            	if (res.code == 1) {
				            		uni.showToast({
				            			icon: 'success',
				            			title: '绑定成功！'
				            		});5
				            		that.user.IMEI = that.code
				            		uni.setStorageSync("userInfo",that.user)
				            	}else{
									uni.showToast({
										icon: 'success',
										title: res.message,
										position:"bottom"
									});
								}
				            })
				        }
				    }
				});
			}
		}
	}
</script>

<style>

</style>
